using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsIMPA_TREE_LOADER
    {
        public clsIMPA_TREE_LOADER(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        public List<IMPA_TREE_LOADER> DataTableToLstObj(DataTable dt)
        {
            IMPA_TREE_LOADER treeLoaderDTO;
            List<IMPA_TREE_LOADER> lstTreeLoaderDTO = new List<IMPA_TREE_LOADER>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                treeLoaderDTO = new IMPA_TREE_LOADER();
                try
                {
                    treeLoaderDTO._itemNo = int.Parse(dt.Rows[i]["ItemNo"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    treeLoaderDTO._l1Code = int.Parse(dt.Rows[i]["L1Code"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    treeLoaderDTO._l1Name = dt.Rows[i]["L1Name"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    treeLoaderDTO._l2Code = int.Parse(dt.Rows[i]["L2Code"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    treeLoaderDTO._l2Name = dt.Rows[i]["L2Name"].ToString();
                }
                catch (Exception ex)
                {
                }
                try
                {
                    treeLoaderDTO._l3Code = int.Parse(dt.Rows[i]["L3Code"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    treeLoaderDTO._l3Name = dt.Rows[i]["L3Name"].ToString();
                }
                catch (Exception ex)
                {
                }
                lstTreeLoaderDTO.Add(treeLoaderDTO);
            }
            return lstTreeLoaderDTO;
        }
        public IMPA_TREE_LOADER DataTableToObj(DataTable dt)
        {
            IMPA_TREE_LOADER treeLoaderDTO;
            treeLoaderDTO = new IMPA_TREE_LOADER();

            try
            {
                treeLoaderDTO._itemNo = int.Parse(dt.Rows[0]["ItemNo"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                treeLoaderDTO._l1Code = int.Parse(dt.Rows[0]["L1Code"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                treeLoaderDTO._l1Name = dt.Rows[0]["L1Name"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                treeLoaderDTO._l2Code = int.Parse(dt.Rows[0]["L2Code"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                treeLoaderDTO._l2Name = dt.Rows[0]["L2Name"].ToString();
            }
            catch (Exception ex)
            {
            }
            try
            {
                treeLoaderDTO._l3Code = int.Parse(dt.Rows[0]["L3Code"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                treeLoaderDTO._l3Name = dt.Rows[0]["L3Name"].ToString();
            }
            catch (Exception ex)
            {
            }

            return treeLoaderDTO;
        }
        public List<IMPA_TREE_LOADER> LoadImpaTreeLoaderL1()
        {
            DataTable dt = new DataTable();
            List<IMPA_TREE_LOADER> lstTreeLoaderDTO = new List<IMPA_TREE_LOADER>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_TREE_LOADER_L1";
                dt = SqlHelper.ExecuteToDataTable(spName, CommandType.StoredProcedure);
                lstTreeLoaderDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTreeLoaderDTO;
        }
        public List<IMPA_TREE_LOADER> LoadImpaTreeLoaderL2ByL1(int l1)
        {
            DataTable dt = new DataTable();
            List<IMPA_TREE_LOADER> lstTreeLoaderDTO = new List<IMPA_TREE_LOADER>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_TREE_LOADER_L2ByL1";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@l1", l1);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstTreeLoaderDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTreeLoaderDTO;
        }
        public List<IMPA_TREE_LOADER> LoadImpaTreeLoaderL3ByL2(int l2)
        {
            DataTable dt = new DataTable();
            List<IMPA_TREE_LOADER> lstTreeLoaderDTO = new List<IMPA_TREE_LOADER>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectIMPA_TREE_LOADER_L3ByL2";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@l2", l2);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstTreeLoaderDTO = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstTreeLoaderDTO;
        }
    }
}
